package ru.susu.algebra.centralunits.alternating.tex;

import java.math.BigInteger;

import ru.susu.algebra.centralunits.CentralUnitsOptions;
import ru.susu.algebra.centralunits.alternating.MathMethodWithInitializers;
import ru.susu.algebra.centralunits.alternating.initializers.SpecialRowsInitializer;
import ru.susu.algebra.chartable.constructor.AlternatingCharTableUtil;
import ru.susu.algebra.jtex.ITexElement;
import ru.susu.algebra.jtex.StringTexElement;
import ru.susu.algebra.jtex.TexBeginEndElement;
import ru.susu.algebra.jtex.formula.FormulaTexElement;
import ru.susu.algebra.jtex.formula.GreekSymbols;
import ru.susu.algebra.jtex.utils.TexUtils;
import ru.susu.algebra.partition.Partition;
import ru.susu.algebra.properties.IPropertySource;
import ru.susu.algebra.util.NumberUtilities;

/**
 * @author akargapolov
 * @since: 26.08.2010
 */
public class ZChiNotationGenerator extends MathMethodWithInitializers<ITexElement>
{
	private static final Class[] DEPENDENCIES = {SpecialRowsInitializer.class};

	@Override
	protected ITexElement directRun(IPropertySource ps) throws Exception
	{
		TexBeginEndElement notation = TexBeginEndElement.notation();

		for (Partition specialRow : SpecialRowsInitializer.listSpecialNotIntegerCharacterRows(ps))
		{
			Integer number = SpecialRowsInitializer.getRowNumber(ps, specialRow);

			String index = TexUtils.index(number + "");
			BigInteger factorial = NumberUtilities.getFactorial(CentralUnitsOptions.getNumber(ps)).divide(BigInteger.valueOf(2));
			BigInteger degree = AlternatingCharTableUtil.calcAlternatingDegree(specialRow);
			String factorialF = TexUtils.getFactorization(factorial);
			String degreeF = TexUtils.getFactorization(degree);
			FormulaTexElement formula = FormulaTexElement.formula("z" + index + " = z\\left(" + GreekSymbols.CHI + index + "\\right) = " +
					FormulaTexElement.frac("|A_{" + CentralUnitsOptions.getNumber(ps) + "}|", FormulaTexElement.DEG + GreekSymbols.CHI + index) + " = \n" +
					FormulaTexElement.frac(factorialF, degree.toString()) + " = \n" +
					FormulaTexElement.frac(factorialF, degreeF) + " = \n" +
					TexUtils.getFactorization(factorial.divide(degree)) + " = " + factorial.divide(degree));
			notation.addSubElement(formula);
			notation.addSubElement(StringTexElement.text("\n\n"));
		}
		return notation;
	}

	@Override
	protected Class[] getDependentInitializers()
	{
		return DEPENDENCIES;
	}

}
